package itsm.isperp.module.controller.bpm;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.BpmRelationProcessMap;
import itsm.isperp.module.entity.bpm.BpmRelationProcessMapField;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.service.bpm.BpmProcessSettingService;
import itsm.isperp.module.service.bpm.BpmRelationProcessMapFieldService;
import itsm.isperp.module.service.bpm.BpmRelationProcessMapService;
import itsm.isperp.module.service.db.DbEntityFieldService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-02-03 11:13
 */
@Controller
@RequestMapping("bpm/relationProcessMapField")
public class BpmRelationProcessMapFieldController extends
		BaseController<BpmRelationProcessMapField> {

	@Autowired
	protected BpmRelationProcessMapFieldService bpmRelationProcessMapFieldService;

	@Override
	public BpmRelationProcessMapFieldService getService() {
		return this.bpmRelationProcessMapFieldService;
	}

	@Override
	public String getPageTitle() {
		return "流程关联映射字段";
	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = super.getEdit(id);
		String relationProcessMapId = req.getParameter("relationProcessMapId");

		BpmRelationProcessMapField bf = (BpmRelationProcessMapField) mv
				.getModel().get("model");
		if (StringUtils.isEmpty(bf.getRelationProcessMapId())) {

			bf.setRelationProcessMapId(relationProcessMapId);
		} else {
			relationProcessMapId = bf.getRelationProcessMapId();
		}

		BpmRelationProcessMapService bpmRelationProcessMapService = ContextHolder
				.getSpringBean("bpmRelationProcessMapService");

		BpmRelationProcessMap bm = bpmRelationProcessMapService
				.findOne(relationProcessMapId);

		BpmProcessSettingService bpmProcessSettingService = ContextHolder
				.getSpringBean("bpmProcessSettingService");

		DbEntity fromEntity = bpmProcessSettingService.findDbEntityByKey(bm
				.getFromProcessKey());

		mv.addObject("fromTableName", fromEntity.getTableName());

		DbEntity toEntity = bpmProcessSettingService.findDbEntityByKey(bm
				.getToProcessKey());

		DbEntityFieldService dbEntityFieldService = ContextHolder
				.getSpringBean("dbEntityFieldService");
		List<DbEntityField> fields = dbEntityFieldService.findAllByEntityId(
				toEntity.getId(), new String[] { "name", "column_name" });

		mv.addObject("fields", fields);

		return mv;
	}

	@Override
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<BpmRelationProcessMapField> list(JqGridRequest request,
			HttpServletRequest req) {
		String relationProcessMapId = req.getParameter("relationProcessMapId");
		request.addFilterRule("relationProcessMapId", JqGridFilterRuleOp.eq,
				relationProcessMapId);

		return this.getService().findAll(request).toPage();

	}

}
